//
//  KKMyUploadController.m
//  DoMi
//
//  Created by 康凯 on 2016/11/11.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKMyUploadController.h"
#import "KKTopSelectView.h"
#import "KKMyHttp.h"
#import "KKArticleDetailVC.h"
#import "KKVideoDetailController.h"



#import "KKCollectDocumentCell.h"
#import "KKBuyVideoCell.h"

@interface KKMyUploadController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//0代表文章1代表视频
@property (nonatomic,assign) int flag;
@property (nonatomic,strong) NSArray *documentDatas;
@property (nonatomic,strong) NSArray *videoDatas;
@end

@implementation KKMyUploadController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的上传";
    [self initView];
}

#pragma mark 初始化view
-(void)initView{
    self.flag=0;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    KKTopSelectView *topItem=[[[NSBundle mainBundle] loadNibNamed:@"KKTopSelectView" owner:nil options:nil] lastObject];
    [topItem initData:@[@"文章",@"视频"]];
    topItem.selectBlock=^(int flag){
        self.flag=flag;
        [self.tableView reloadData];
    };
    [self.topView addSubview:topItem];
    
    //取消group样式头部空白
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 0.1f)];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKCollectDocumentCell" bundle:nil] forCellReuseIdentifier:@"KKCollectDocumentCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKBuyVideoCell" bundle:nil] forCellReuseIdentifier:@"KKBuyVideoCell"];
    
    [self getUploadDocument:@"1"];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==0){
        KKCollectDocumentCell *document= (KKCollectDocumentCell *)cell;
        [document initUploadData:self.documentDatas[indexPath.section]];
    }else{
        KKBuyVideoCell *video= (KKBuyVideoCell *)cell;
        [video initData:self.videoDatas[indexPath.section]];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(self.flag==0){
        return self.documentDatas.count;
    }else{
        return self.videoDatas.count;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==0){//文章
        return [tableView dequeueReusableCellWithIdentifier:@"KKCollectDocumentCell"];
    }else if(self.flag==1){//视频
        return [tableView dequeueReusableCellWithIdentifier:@"KKBuyVideoCell"];
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 8;
    }
    return 0.1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==0){
        KKCollectDocumentM *doc=self.documentDatas[indexPath.section];
        KKArticleDetailVC *detail=[[KKArticleDetailVC alloc] init];
        detail.docId=doc.articleid;
        [self.navigationController pushViewController:detail animated:YES];
    }else{
        KKCollectVideoM *video=self.videoDatas[indexPath.section];
        KKVideoDetailController *detail=[[KKVideoDetailController alloc]init];
        detail.videoId=video.videoid;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.flag==0){
        KKCollectDocumentM *documents=self.documentDatas[indexPath.section];
        [self deleteUpload:documents.articleid];
    }else{
//        KKCollectVideoM *documents=self.videoDatas[indexPath.section];
//        [self deleteUpload:documents.collectid];
    }
    
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

#pragma mark 接口--收藏文章
-(void)getUploadDocument:(NSString *)page{
    [self showDialog];
    [KKMyHttp getUploadDocument:page success:^(id data) {
        self.documentDatas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}

#pragma mark 接口--删除收藏收藏
-(void)deleteUpload:(NSString *)uploadId{
    [KKMyHttp deleteDocument:uploadId success:^(id data) {
        if(self.flag==0){
            [self getUploadDocument:@"1"];
        }else{
//            [self getCollectVideo:@"1"];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
